home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 July: Mac OS SDK / Dev.CD Jul 96 SDK / Dev.CD Jul 96 SDK1.toast / Development Kits (Disc 1) / OpenDoc Development Framework / ODFDev / Draw / Sources / DrawPart.cpp < prev    next >
Encoding:
Text File  |  1996-04-25  |  29.0 KB  |  1,062 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                DrawPart.cpp
  4. //    Release Version:    $ ODF 1 $
  5. //
  6. //    Author:                Henri Lamiraux
  7. //
  8. //    Copyright:    (c) 1993 - 1996 by Apple Computer, Inc., all rights reserved.
  9. //
  10. //========================================================================================
  11.  
  12. #include "ODFDraw.hpp"
  13.  
  14. #ifndef DRAWPART_H
  15. #include "DrawPart.h"
  16. #endif
  17.  
  18. #ifndef DEFINES_K
  19. #include "Defines.k"
  20. #endif
  21.  
  22. #ifndef BINDING_K
  23. #include "Binding.k"
  24. #endif
  25.  
  26. #ifndef CONSTANT_H
  27. #include "Constant.h"
  28. #endif
  29.  
  30. #ifndef DEFINES_K
  31. #include "Defines.k"
  32. #endif
  33.  
  34. #ifndef DRAWCONT_H
  35. #include "DrawCont.h"
  36. #endif
  37.  
  38. #ifndef DRAWFRM_H
  39. #include "DrawFrm.h"
  40. #endif
  41.  
  42. #ifndef DRAWPRXY_H
  43. #include "DrawPrxy.h"
  44. #endif
  45.  
  46. #ifndef BASESHP_H
  47. #include "BaseShp.h"
  48. #endif
  49.  
  50. #ifndef BOUNDSHP_H
  51. #include "BoundShp.h"
  52. #endif
  53.  
  54. #ifndef LINESHP_H
  55. #include "LineShp.h"
  56. #endif
  57.  
  58. #ifndef OVALSHP_H
  59. #include "OvalShp.h"
  60. #endif
  61.  
  62. #ifndef RECTSHP_H
  63. #include "RectShp.h"
  64. #endif
  65.  
  66. #ifndef RRECTSHP_H
  67. #include "RRectShp.h"
  68. #endif
  69.  
  70. #ifndef TEXTSHP_H
  71. #include "TextShp.h"
  72. #endif
  73.  
  74. #ifndef DRAWSEL_H
  75. #include "DrawSel.h"
  76. #endif
  77.  
  78. #ifndef DRAWLINK_H
  79. #include "DrawLink.h"
  80. #endif
  81.  
  82. #ifndef TOOLFRM_H
  83. #include "ToolFrm.h"
  84. #endif
  85.  
  86. #ifndef PALFRAME_H
  87. #include "PalFrame.h"
  88. #endif
  89.  
  90. #ifndef PATFRAME_H
  91. #include "PatFrame.h"
  92. #endif
  93.  
  94. #ifndef DRAWCMDS_H
  95. #include "DrawCmds.h"
  96. #endif
  97.  
  98. #ifndef DRAWCLIP_H
  99. #include "DrawClip.h"
  100. #endif
  101.  
  102. #ifndef GROUPCMD_H
  103. #include "GroupCmd.h"
  104. #endif
  105.  
  106. #ifndef SOM_ODFExamples_ODFDrawEmbeddedFramesIterator_xh
  107. #include "SOMIter.xh"
  108. #endif
  109.  
  110. //#ifndef ALGNOBFR_H
  111. //#include "AlgnObFr.h"
  112. //#endif
  113.  
  114. // ----- Part Layer -----
  115.  
  116. #ifndef FWABOUT_H
  117. #include "FWAbout.h"
  118. #endif
  119.  
  120. #ifndef FWUTIL_H
  121. #include "FWUtil.h"
  122. #endif
  123.  
  124. #ifndef FWSCLBAR_H
  125. #include "FWSclBar.h"
  126. #endif
  127.  
  128. #ifndef FWPRESEN_H
  129. #include "FWPresen.h"
  130. #endif
  131.  
  132. // ----- OS Layer -----
  133.  
  134. #ifndef FWAPLEVT_H
  135. #include "FWAplEvt.h"
  136. #endif
  137.  
  138. #ifndef FWRECT_H
  139. #include "FWRect.h"
  140. #endif
  141.  
  142. #ifndef FWRECSHP_H
  143. #include "FWRecShp.h"
  144. #endif
  145.  
  146. #ifndef FWWINDOW_H
  147. #include "FWWindow.h"
  148. #endif
  149.  
  150. #ifndef FWFLOWIN_H
  151. #include "FWFloWin.h"
  152. #endif
  153.  
  154. #ifndef FWEVENT_H
  155. #include "FWEvent.h"
  156. #endif
  157.  
  158. #ifndef FWMENU_H
  159. #include "FWMenu.h"
  160. #endif
  161.  
  162. #ifndef FWODGEOM_H
  163. #include "FWODGeom.h"
  164. #endif
  165.  
  166. #ifndef FWALERT_H
  167. #include "FWAlert.h"
  168. #endif
  169.  
  170. #ifndef FWCFMRES_H
  171. #include "FWCFMRes.h"
  172. #endif
  173.  
  174. #ifndef SLMixOS_H
  175. #include "SLMixOS.h"
  176. #endif
  177.  
  178. // ----- Foundation Includes -----
  179.  
  180. #ifndef FWSTREAM_H
  181. #include "FWStream.h"
  182. #endif
  183.  
  184. #ifndef FWSTRING_H
  185. #include "FWString.h"
  186. #endif
  187.  
  188. #ifndef FWSUSINK_H
  189. #include "FWSUSink.h"
  190. #endif
  191.  
  192. // ----- OpenDoc Includes -----
  193.  
  194. #ifndef SOM_ODArbitrator_xh
  195. #include <Arbitrat.xh>
  196. #endif
  197.  
  198. #ifndef SOM_ODDispatcher_xh
  199. #include <Disptch.xh>
  200. #endif
  201.  
  202. #ifndef SOM_ODShape_xh
  203. #include <Shape.xh>
  204. #endif
  205.  
  206. #ifndef SOM_ODStorageUnit_xh
  207. #include <StorageU.xh>
  208. #endif
  209.  
  210. #ifndef SOM_ODSession_xh
  211. #include <ODSessn.xh>
  212. #endif
  213.  
  214. #ifndef SOM_Module_OpenDoc_StdProps_defined
  215. #include <StdProps.xh>
  216. #endif
  217.  
  218. #ifndef SOM_ODInfo_xh
  219. #include <Info.xh>
  220. #endif
  221.  
  222. //========================================================================================
  223. // Runtime Information
  224. //========================================================================================
  225.  
  226. #ifdef FW_BUILD_MAC
  227. #pragma segment odfdraw
  228. #endif
  229.  
  230. FW_DEFINE_CLASS_M2(CDrawPart, FW_CEmbeddingPart, FW_MEmbeddingPartScriptable)
  231. FW_DEFINE_AUTO(CDrawPart)
  232.  
  233. //========================================================================================
  234. //    class CDrawPart
  235. //========================================================================================
  236.  
  237. //----------------------------------------------------------------------------------------
  238. //    CDrawPart::CDrawPart
  239. //----------------------------------------------------------------------------------------
  240.  
  241. CDrawPart::CDrawPart(ODPart* odPart) :
  242.     FW_CEmbeddingPart(odPart, FW_gInstance, kPartInfoID),
  243.     FW_MEmbeddingPartScriptable(),
  244.     fNbEmbedded(0),    
  245.     fPartContent(NULL),
  246.     fCurrentPenSize(FW_kFixedPos1),
  247.     fRenderVerb(kFillFrame),
  248.     fPaletteWindow(NULL),
  249.     fPatternWindow(NULL),
  250.     fToolsWindow(NULL),
  251.     fToolFrame(NULL),
  252.     fPaletteFrame(NULL),
  253.     fPatternFrame(NULL),
  254.     fPalettePresentation(NULL),
  255.     fPatternPresentation(NULL),
  256.     fToolPresentation(NULL),
  257.     fMainPresentation(NULL),
  258.     fAlignObjectsPresentation(NULL),    
  259.     fTool(kSelectTool),    
  260.     fFillColor(FW_kRGBWhite),
  261.     fFrameColor(FW_kRGBBlack),    
  262.     fAutoGrid(TRUE),
  263.     fUnit(cInchUnit),
  264.     fArcUnit(cDegreesUnit),
  265.     fPostCreated(FALSE)
  266. {
  267.     
  268.     FW_END_CONSTRUCTOR
  269. }
  270.  
  271. //----------------------------------------------------------------------------------------
  272. //    CDrawPart::~CDrawPart
  273. //----------------------------------------------------------------------------------------
  274.  
  275. CDrawPart::~CDrawPart()
  276. {
  277.     FW_START_DESTRUCTOR
  278. }
  279.  
  280. //----------------------------------------------------------------------------------------
  281. //    CDrawPart::ReleaseAll
  282. //----------------------------------------------------------------------------------------
  283.  
  284. void CDrawPart::ReleaseAll(Environment *ev)
  285. {
  286.     FW_CEmbeddingPart::ReleaseAll(ev);
  287.     
  288.     delete fPaletteWindow;
  289.     delete fPatternWindow;
  290.     delete fToolsWindow;
  291. }
  292.  
  293. //----------------------------------------------------------------------------------------
  294. //    CDrawPart::Initialize
  295. //----------------------------------------------------------------------------------------
  296.  
  297. void CDrawPart::Initialize(Environment *ev)
  298. {
  299.     // ----- Call Inherited Initialize -----
  300.     FW_CEmbeddingPart::Initialize(ev);
  301.  
  302.     fDrawingSize.Set(FW_IntToFixed(kDrawingSizeX), FW_IntToFixed(kDrawingSizeY));
  303.  
  304. #ifdef FW_BUILD_WIN
  305.     // [HLX] Temporary embed menu for windows
  306.     WinAddEmbedMenu(ev, GetMenuBar(ev));
  307. #endif
  308.  
  309.     // ----- Initialize the menu bar -----
  310.     FW_DO_NOT_DEAD_STRIP(FW_CToggleItem);
  311.     FW_DO_NOT_DEAD_STRIP(FW_CSubMenuItem);
  312.     GetMenuBar(ev)->InitializeFromResource(ev, kMenuBar);
  313.  
  314.     // ----- Tokenize -----
  315.     fDrawSelection = FW_NEW(CDrawSelection, (ev, this));    // Attention I don't own the selection the presentation will
  316.     
  317.     fToolPresentation             = RegisterPresentation(ev, kToolPresentation, FALSE);
  318.     fPalettePresentation         = RegisterPresentation(ev, kPalettePresentation, FALSE);
  319.     fPatternPresentation         = RegisterPresentation(ev, kPatternPresentation, FALSE);
  320.     fMainPresentation             = RegisterPresentation(ev, kMainPresentation, TRUE, fDrawSelection);
  321.     fAlignObjectsPresentation     = RegisterPresentation(ev, kAlignObjectsPresentation, FALSE);
  322.     
  323.     InitializePalettes(ev);
  324.  
  325.     // ----- Pattern Table -----
  326.     fPatternTable[0]     = FW_kBlackPat;
  327.     fPatternTable[1]     = FW_kWhitePat;
  328.     fPatternTable[2]     = FW_kGrayPat;
  329.     fPatternTable[3]     = FW_kLightGrayPat;
  330.     fPatternTable[4]     = FW_kDarkGrayPat;
  331.     fPatternTable[5]     = FW_kHorizontalPat;
  332.     fPatternTable[6]     = FW_kVerticalPat;
  333.     fPatternTable[7]     = FW_kFDiagonalPat;
  334.     fPatternTable[8]     = FW_kBDiagonalPat;
  335.     fPatternTable[9]     = FW_kCrossPat;
  336.     fPatternTable[10]     = FW_kDiagCrossPat;
  337.     fPatternTable[11]     = FW_kAntPat;
  338.     
  339.     fFillPatIndex = 0;
  340.     fFramePatIndex = 0;
  341. }
  342.  
  343. //------------------------------------------------------------------------------
  344. //    CDrawPart::InitializePalettes
  345. //------------------------------------------------------------------------------
  346.  
  347. void CDrawPart::InitializePalettes(Environment *ev)
  348. {
  349.     FW_CPoint position(FW_IntToFixed(100), FW_IntToFixed(100));
  350.     fPatternWindow = new FW_CFloatingWindow(ev, 
  351.                                             this, 
  352.                                             fPatternPresentation, 
  353.                                             FW_CString32(""), 
  354.                                             FW_kZeroPoint, 
  355.                                             position,
  356.                                             TRUE);
  357.  
  358.     fPaletteWindow = new FW_CFloatingWindow(ev, 
  359.                                             this,
  360.                                             fPalettePresentation, 
  361.                                             FW_CString32(""), 
  362.                                             FW_kZeroPoint, 
  363.                                             position,
  364.                                             TRUE);
  365.  
  366.     FW_CPoint windowSize(FW_IntToFixed(kToolsWindowWidth), FW_IntToFixed(kToolsWindowHeight));
  367.     fToolsWindow = new FW_CFloatingWindow(ev, 
  368.                                             this, 
  369.                                             fToolPresentation, 
  370.                                             FW_CString32(""), 
  371.                                             windowSize, 
  372.                                             position,
  373.                                             TRUE);
  374. }
  375.  
  376. //----------------------------------------------------------------------------------------
  377. //    CDrawPart::NewDocumentWindow
  378. //----------------------------------------------------------------------------------------
  379.  
  380. FW_CWindow* CDrawPart::NewDocumentWindow(Environment* ev)
  381. {
  382.     FW_CPoint sbSize = FW_CScrollBar::GetDefaultScrollBarSize();
  383.  
  384.     // ----- Add the rulers -----
  385.     sbSize.x += kRulerWidth;
  386.     sbSize.y += kRulerWidth;
  387.     
  388.     FW_CPoint drawingSize(fDrawingSize.x + sbSize.x, fDrawingSize.y + sbSize.y);
  389.         
  390.     FW_CRect screenBounds;
  391.     ::FW_GetMainScreenBounds(screenBounds);
  392.     screenBounds.Inset(FW_IntToFixed(3), FW_IntToFixed(3));
  393.     
  394.     return new FW_CWindow(ev,
  395.                         this,
  396.                          FW_CPart::gViewAsFrameToken,
  397.                         fMainPresentation,
  398.                         drawingSize,
  399.                         screenBounds.TopLeft(),
  400.                         FW_kDocumentWindow);
  401. }
  402.  
  403. //----------------------------------------------------------------------------------------
  404. //    CDrawPart::NewPartContent
  405. //----------------------------------------------------------------------------------------
  406.  
  407. FW_CContent* CDrawPart::NewPartContent(Environment* ev)
  408. {
  409.     fPartContent = FW_NEW(CDrawPartContent, (ev, this));
  410.     return fPartContent;
  411. }
  412.  
  413. //----------------------------------------------------------------------------------------
  414. //    CDrawPart::InternalizeAnnotations
  415. //----------------------------------------------------------------------------------------
  416.  
  417. void CDrawPart::InternalizeAnnotations(Environment *ev, ODStorageUnit* storageUnit)
  418. {
  419.     // ----- Read general information -----
  420.     if (storageUnit->Exists(ev, kContentAnnotationProp, (ODValueType)kContentAnnotationValue, 0))
  421.     {
  422.         FW_PStorageUnitSink suSink(ev, storageUnit, kContentAnnotationProp, kContentAnnotationValue);
  423.         FW_PBufferedSink sink(ev, suSink);
  424.         FW_CReadableStream stream(sink);
  425.         stream >> fCurrentPenSize;
  426.         stream >> fRenderVerb;
  427.         stream >> fAutoGrid;
  428.     }
  429.  
  430.     // ----- Now we have read everything in we can call the facet clipper -----
  431.     CDrawFacetClipper facetClipper(ev, this);
  432.     facetClipper.Clip(ev, fMainPresentation, NULL);
  433. }
  434.  
  435. //----------------------------------------------------------------------------------------
  436. //    CDrawPart::ExternalizeAnnotations
  437. //----------------------------------------------------------------------------------------
  438. void CDrawPart::ExternalizeAnnotations(Environment* ev, ODStorageUnit* storageUnit)
  439. {
  440.     // ----- Write general information -----
  441.     //    FW_SUAddPropValue correctly test if the property and value exist before
  442.     //    trying to create them
  443.     FW_SUAddPropValue(ev, storageUnit, kContentAnnotationProp, kContentAnnotationValue);
  444.  
  445.     FW_PStorageUnitSink suSink(ev, storageUnit, kContentAnnotationProp, kContentAnnotationValue);
  446.     FW_CWritableStream stream(suSink);
  447.     stream << fCurrentPenSize;
  448.     stream << fRenderVerb;
  449.     stream << fAutoGrid;
  450. }
  451.  
  452. //----------------------------------------------------------------------------------------
  453. //    CDrawPart::NewLinkManager
  454. //----------------------------------------------------------------------------------------
  455. FW_CLinkManager* CDrawPart::NewLinkManager(Environment *ev)
  456. {
  457.     CDrawLinkManager* linkMgr = FW_NEW(CDrawLinkManager, (ev, this));
  458.     return linkMgr;
  459. }
  460.  
  461. //----------------------------------------------------------------------------------------
  462. //    CDrawPart::AddShapeToPart
  463. //----------------------------------------------------------------------------------------
  464.  
  465. void CDrawPart::AddShapeToPart(Environment* ev, CBaseShape* shape)
  466. {
  467.     fPartContent->AddShape(ev, shape);
  468. }
  469.  
  470. //----------------------------------------------------------------------------------------
  471. //    CDrawPart::RemoveShapeFromPart
  472. //----------------------------------------------------------------------------------------
  473.  
  474. void CDrawPart::RemoveShapeFromPart(Environment* ev, CBaseShape* shape)
  475. {
  476.     fPartContent->RemoveShape(ev, shape);
  477.     shape->Removed(ev);
  478. }
  479.  
  480. //----------------------------------------------------------------------------------------
  481. //    CDrawPart::DoMenu
  482. //----------------------------------------------------------------------------------------
  483.  
  484. FW_Boolean CDrawPart::DoMenu(Environment* ev, const FW_CMenuEvent& theMenuEvent)
  485. {
  486.     FW_CString32 name("ODFDraw");
  487.     FW_CString32 sorry("Sorry, not yet implemented!");
  488.  
  489.     FW_Boolean result = TRUE;
  490.     ODCommandID commandID = theMenuEvent.GetCommandID(ev);
  491.     
  492.     if (cPen1 <= commandID && commandID <= cPen5)
  493.     {
  494.         int temp = cPen1;
  495.         FW_Fixed penSize = FW_IntToFixed(commandID - temp + 1);
  496.         if (!fDrawSelection->IsEmpty(ev))
  497.         {
  498.             CChangePenSizeCommand* cmd = FW_NEW(CChangePenSizeCommand, (ev, this, this->GetLastActiveFrame(ev), fDrawSelection, penSize));
  499.             cmd->Execute(ev);
  500.         }
  501.         else
  502.             fCurrentPenSize = penSize;
  503.     }
  504.     else
  505.     {
  506.         switch (commandID)
  507.         {
  508.             case kODCommandAbout:
  509.                 ::FW_About(ev, this, kAbout);
  510.                 break;
  511.                 
  512.             case cHideShowPattern:
  513.                 fPatternWindow->ShowHide(ev, !fPatternWindow->IsShown(ev));
  514.                 break;
  515.                 
  516.             case cHideShowPalette:
  517.                 fPaletteWindow->ShowHide(ev, !fPaletteWindow->IsShown(ev));
  518.                 break;
  519.             
  520.             case cHideShowTools:
  521.                 fToolsWindow->ShowHide(ev, !fToolsWindow->IsShown(ev));
  522.                 break;
  523.  
  524.             case cMoveForward:
  525.                 if (fPartContent->MoveForward(ev))
  526.                 {
  527.                     UpdateAfterMove(ev);
  528.                     this->Changed(ev);
  529.                 }
  530.                 break;
  531.             case cMoveToFront:
  532.                 fPartContent->MoveToFront(ev);
  533.                 UpdateAfterMove(ev);
  534.                 this->Changed(ev);
  535.                 break;
  536.             case cMoveBackward:
  537.                 if (fPartContent->MoveBackward(ev))
  538.                 {
  539.                     UpdateAfterMove(ev);
  540.                     this->Changed(ev);
  541.                 }
  542.                 break;
  543.             case cMoveToBack:
  544.                 fPartContent->MoveToBack(ev);
  545.                 UpdateAfterMove(ev);
  546.                 this->Changed(ev);
  547.                 break;
  548.             
  549.             case cAutoGrid:
  550.                 fAutoGrid = !fAutoGrid;
  551.                 break;
  552.  
  553.             case cAlignObjects:
  554. #ifdef FW_DIALOGS_WIP
  555.                 LaunchDialog(ev, fAlignObjectsPresentation, kModalDialog,
  556.                               FW_CPoint(ff(100), ff(100)), FW_CPoint(ff(360), ff(220)));
  557. #endif
  558. //                break;
  559.                 
  560.             case cGroup:
  561.               {
  562.                 CGroupShapesCommand* cmd = FW_NEW(CGroupShapesCommand, (ev, this->GetLastActiveFrame(ev), fDrawSelection));
  563.                 cmd->Execute(ev);
  564.               }
  565.                 break;
  566.             case cUngroup:
  567.               {
  568.                 CUngroupShapesCommand* cmd = FW_NEW(CUngroupShapesCommand, (ev, this->GetLastActiveFrame(ev), fDrawSelection));
  569.                 cmd->Execute(ev);
  570.               }
  571.                 break;
  572.  
  573.             case cAlignToGrid:
  574.             case cRotate:
  575.             case cFlipH:
  576.             case cFlipV:
  577.             case cLock:
  578.             case cUnlock:
  579.             case cScaleSelection:
  580.             case cRoundCorners:
  581.                 FW_NoteAlert(name, sorry);
  582.                 break;
  583.  
  584.             case cInchUnit:
  585.             case cCmUnit:
  586.             case cPixelUnit:
  587.             case cPicaUnit:
  588.                 fUnit = commandID;
  589.                 FW_NoteAlert(name, sorry);
  590.                 break;
  591.                 
  592.             case cDegreesUnit:
  593.             case cRadiansUnit:
  594.                 fArcUnit = commandID;
  595.                 FW_NoteAlert(name, sorry);
  596.                 break;
  597.  
  598.             default:
  599.                 result = FALSE;
  600.         }    
  601.     }
  602.     
  603.     return result;
  604. }
  605.  
  606. //----------------------------------------------------------------------------------------
  607. //    CDrawPart::SetTool
  608. //----------------------------------------------------------------------------------------
  609. void CDrawPart::SetTool(Environment* ev, unsigned short newTool)
  610. {
  611.     if (fTool != newTool)
  612.     {
  613.         if (fTool == kSelectTool && !fDrawSelection->IsEmpty(ev))
  614.             fDrawSelection->CloseSelection(ev);
  615.         
  616.         CheckTool(ev, fTool, newTool);
  617.         fTool = newTool;        
  618.     }
  619. }
  620.  
  621. //----------------------------------------------------------------------------------------
  622. //    CDrawPart::SetRenderVerb
  623. //----------------------------------------------------------------------------------------
  624.  
  625. void CDrawPart::SetRenderVerb(Environment* ev, unsigned short renderVerb)
  626. {
  627.     if (fRenderVerb != renderVerb)
  628.     {        
  629.         if (!fDrawSelection->IsEmpty(ev))
  630.         {
  631.             CChangeRenderVerbCommand* cmd = FW_NEW(CChangeRenderVerbCommand,
  632.                                                     (ev, this, this->GetLastActiveFrame(ev), fDrawSelection, renderVerb));
  633.             cmd->Execute(ev);
  634.         }
  635.  
  636.         unsigned short oldRenderVerb = fRenderVerb;
  637.         fRenderVerb = renderVerb;        
  638.         CheckRenderVerb(ev, oldRenderVerb, fRenderVerb);
  639.     }
  640. }
  641.  
  642. //----------------------------------------------------------------------------------------
  643. //    CDrawPart::NewFrame
  644. //----------------------------------------------------------------------------------------
  645.  
  646. FW_CFrame* CDrawPart::NewFrame(Environment* ev, 
  647.                                 ODFrame* odFrame, 
  648.                                 FW_CPresentation* presentation,
  649.                                 FW_Boolean fromStorage)
  650. {
  651.     if (presentation == fPalettePresentation)
  652.     {
  653.         if (fPaletteFrame)
  654.             return fPaletteFrame;
  655.         else
  656.             return (fPaletteFrame = FW_NEW(CPaletteFrame, (ev, odFrame, presentation, this)));
  657.     }
  658.     else if (presentation == fPatternPresentation)
  659.     {
  660.         if (fPatternFrame)
  661.             return fPatternFrame;
  662.         else
  663.             return (fPatternFrame = FW_NEW(CPatternFrame, (ev, odFrame, presentation, this)));
  664.     }
  665.     else if (presentation == fToolPresentation)
  666.     {
  667.         if (fToolFrame)
  668.             return fToolFrame;
  669.         else
  670.             return (fToolFrame = FW_NEW(CToolFrame, (ev, odFrame, presentation, this)));
  671.     }
  672. //    else if (presentation == fAlignObjectsPresentation)
  673. //    {
  674. //        return FW_NEW(CAlignObjectsFrame, (ev, odFrame, this));
  675. //    }
  676.     
  677.     // Create CDrawFrame from resources, using a different set of resources depending on
  678.     // whether this frame is embedded or not to avoid creating a grow-box and scroll-bars.
  679.     // NOTE: Another solution is to use only 1 set of resources and remove the grow-box
  680.     // and scroll-bars in HandlePostCreateViewFromStream().  See the Container sample.
  681.     FW_ResourceId id = odFrame->IsRoot(ev) ? kDrawView : kDrawViewNotRoot;
  682.     
  683.     return FW_NEW(CDrawFrame, (ev, odFrame, presentation, this, id));
  684. }
  685.  
  686. //----------------------------------------------------------------------------------------
  687. //    CDrawPart::DoAdjustMenus
  688. //----------------------------------------------------------------------------------------
  689.  
  690. FW_Boolean CDrawPart::DoAdjustMenus(Environment* ev, FW_CMenuBar* menuBar, FW_Boolean hasMenuFocus, FW_Boolean isRoot)
  691. {
  692.     if (!hasMenuFocus)
  693.         return FALSE;
  694.         
  695.     ODCommandID command;
  696.     FW_Boolean hasSelection = fDrawSelection->Count() > 0;
  697.  
  698.     // ----- Edit Menu -----
  699.     menuBar->EnableCommand(ev, kODCommandSelectAll, fPartContent->CountShapes() > 0);
  700.  
  701.     // ----- PenSize Menu -----
  702.     FW_Fixed penSize = fCurrentPenSize;
  703.     if (fDrawSelection->Count() >= 1)
  704.     {
  705.         if (!fDrawSelection->GetSelectionPenSize(ev, penSize))
  706.         {
  707.             penSize = FW_kFixed0;
  708.         }
  709.     }
  710.     command = FW_FixedToInt(penSize) + cPen1 - 1;
  711.     
  712.     for (ODCommandID p = cPen1; p <= cPen5; p++)
  713.         menuBar->EnableAndCheckCommand(ev, p, TRUE, p == command);
  714.     
  715.     FW_Boolean readWrite = !IsReadOnly(ev);
  716.     
  717.     // ----- Arrange Menu -----
  718.     menuBar->EnableCommand(ev, cMoveForward, readWrite && hasSelection);
  719.     menuBar->EnableCommand(ev, cMoveToFront, readWrite && hasSelection);
  720.     menuBar->EnableCommand(ev, cMoveBackward, readWrite && hasSelection);
  721.     menuBar->EnableCommand(ev, cMoveToBack, readWrite && hasSelection);
  722.     menuBar->EnableCommand(ev, cAlignToGrid, readWrite && hasSelection);
  723.     menuBar->EnableCommand(ev, cAlignObjects, readWrite && hasSelection);
  724.     menuBar->EnableCommand(ev, cRotate, readWrite && hasSelection);
  725.     menuBar->EnableCommand(ev, cFlipH, readWrite && hasSelection);
  726.     menuBar->EnableCommand(ev, cFlipV, readWrite && hasSelection);
  727.     menuBar->EnableCommand(ev, cGroup, readWrite && hasSelection);
  728.     menuBar->EnableCommand(ev, cUngroup, readWrite && hasSelection);
  729.     menuBar->EnableCommand(ev, cLock, readWrite && hasSelection);
  730.     menuBar->EnableCommand(ev, cUnlock, readWrite && hasSelection);
  731.         
  732.     // ----- Options Menu -----
  733.     menuBar->EnableAndToggleCommand(ev, cAutoGrid, TRUE, !fAutoGrid);
  734.     menuBar->EnableCommand(ev, cScaleSelection, readWrite && hasSelection);
  735.     menuBar->EnableCommand(ev, cRoundCorners, TRUE);
  736.  
  737.     // ----- Units Menu -----
  738.     menuBar->EnableAndCheckCommand(ev, cInchUnit, TRUE, fUnit == cInchUnit);
  739.     menuBar->EnableAndCheckCommand(ev, cCmUnit, TRUE, fUnit == cCmUnit);
  740.     menuBar->EnableAndCheckCommand(ev, cPixelUnit, TRUE, fUnit == cPixelUnit);
  741.     menuBar->EnableAndCheckCommand(ev, cPicaUnit, TRUE, fUnit == cPicaUnit);
  742.     menuBar->EnableAndCheckCommand(ev, cDegreesUnit, TRUE, fArcUnit == cDegreesUnit);
  743.     menuBar->EnableAndCheckCommand(ev, cRadiansUnit, TRUE, fArcUnit == cRadiansUnit);
  744.  
  745.     // ----- Window Menu -----
  746.     menuBar->EnableAndToggleCommand(ev, cHideShowTools, TRUE, fToolsWindow == NULL || !fToolsWindow->IsShown(ev));
  747.     menuBar->EnableAndToggleCommand(ev, cHideShowPalette, TRUE, fPaletteWindow == NULL || !fPaletteWindow->IsShown(ev));
  748.     menuBar->EnableAndToggleCommand(ev, cHideShowPattern, TRUE, fPatternWindow == NULL || !fPatternWindow->IsShown(ev));    
  749.  
  750.     return FALSE;
  751. }
  752.  
  753. //----------------------------------------------------------------------------------------
  754. //    CDrawPart::CheckTool
  755. //----------------------------------------------------------------------------------------
  756.  
  757. void CDrawPart::CheckTool(Environment* ev, unsigned short oldTool, unsigned short newTool)
  758. {
  759.     if (fToolFrame != NULL)
  760.         fToolFrame->CheckTool(ev, oldTool, newTool);
  761. }
  762.  
  763. //----------------------------------------------------------------------------------------
  764. //    CDrawPart::CheckRenderVerb
  765. //----------------------------------------------------------------------------------------
  766.  
  767. void CDrawPart::CheckRenderVerb(Environment* ev, unsigned short oldRenderVerb, unsigned short newRenderVerb)
  768. {
  769.     if (fToolFrame != NULL)
  770.         fToolFrame->CheckRenderVerb(ev, oldRenderVerb, newRenderVerb);
  771. }
  772.  
  773. //----------------------------------------------------------------------------------------
  774. //    CDrawPart::UpdateAfterMove
  775. //----------------------------------------------------------------------------------------
  776.  
  777. void CDrawPart::UpdateAfterMove(Environment* ev)
  778. {
  779.     CDrawFacetClipper facetClipper(ev, this);
  780.     facetClipper.Clip(ev, fMainPresentation, fDrawSelection->GetUpdateShape());
  781.     
  782.     fMainPresentation->Invalidate(ev, fDrawSelection->GetUpdateShape());    
  783. }
  784.  
  785. //----------------------------------------------------------------------------------------
  786. //    CDrawPart::UpdateAllToolFacets
  787. //----------------------------------------------------------------------------------------
  788.  
  789. void CDrawPart::UpdateAllToolFacets(Environment *ev)
  790. {
  791.     if (fToolFrame != NULL)
  792.         fToolFrame->UpdateColors(ev);
  793. }
  794.  
  795. //----------------------------------------------------------------------------------------
  796. //    CDrawPart::SetFillColor
  797. //----------------------------------------------------------------------------------------
  798.  
  799. void CDrawPart::SetFillColor(Environment *ev, const FW_CColor& color)
  800. {
  801.     if (fDrawSelection->IsEmpty(ev))
  802.     {
  803.         fFillColor = color;
  804.         UpdateAllToolFacets(ev);
  805.     }
  806.     else if (!fDrawSelection->IsOKtoEdit(ev))
  807.     {
  808.         FW_Beep();
  809.     }
  810.     else
  811.     {
  812.         CChangeColorCommand* cmd = FW_NEW(CChangeColorCommand,
  813.                                             (ev, cChangeFillColor, this, this->GetLastActiveFrame(ev), fDrawSelection, color));
  814.         cmd->Execute(ev);
  815.     }
  816. }
  817.  
  818. //----------------------------------------------------------------------------------------
  819. //    CDrawPart::SetFrameColor
  820. //----------------------------------------------------------------------------------------
  821.  
  822. void CDrawPart::SetFrameColor(Environment *ev, const FW_CColor& color)
  823. {    
  824.     if (fDrawSelection->IsEmpty(ev))
  825.     {
  826.         fFrameColor = color;
  827.         UpdateAllToolFacets(ev);
  828.     }
  829.     else if (!fDrawSelection->IsOKtoEdit(ev))
  830.     {
  831.         FW_Beep();
  832.     }
  833.     else
  834.     {    
  835.         CChangeColorCommand* cmd = FW_NEW(CChangeColorCommand,
  836.                                                 (ev, cChangeFrameColor, this, this->GetLastActiveFrame(ev), fDrawSelection, color));
  837.         cmd->Execute(ev);
  838.     }
  839. }
  840.  
  841. //----------------------------------------------------------------------------------------
  842. //    CDrawPart::SetFillPattern
  843. //----------------------------------------------------------------------------------------
  844.  
  845. void CDrawPart::SetFillPattern(Environment *ev, short newFillPatIndex)
  846. {
  847.     if (fDrawSelection->IsEmpty(ev))
  848.     {
  849.         fFillPatIndex = newFillPatIndex;
  850.         UpdateAllToolFacets(ev);
  851.     }
  852.     else if (!fDrawSelection->IsOKtoEdit(ev))
  853.     {
  854.         FW_Beep();
  855.     }
  856.     else
  857.     {
  858.         FW_CPattern pattern = GetPattern(newFillPatIndex).Copy();
  859.         CChangePatternCommand* cmd = FW_NEW(CChangePatternCommand,
  860.                                                 (ev, cChangeFillPattern, this, this->GetLastActiveFrame(ev), fDrawSelection, pattern));
  861.         cmd->Execute(ev);
  862.     }
  863. }
  864.  
  865. //----------------------------------------------------------------------------------------
  866. //    CDrawPart::SetFramePattern
  867. //----------------------------------------------------------------------------------------
  868.  
  869. void CDrawPart::SetFramePattern(Environment *ev, short newFramePatIndex)
  870. {
  871.     if (fDrawSelection->IsEmpty(ev))
  872.     {
  873.         fFramePatIndex = newFramePatIndex;
  874.         UpdateAllToolFacets(ev);
  875.     }
  876.     else if (!fDrawSelection->IsOKtoEdit(ev))
  877.     {
  878.         FW_Beep();
  879.     }
  880.     else
  881.     {
  882.         FW_CPattern pattern = GetPattern(newFramePatIndex).Copy();
  883.         CChangePatternCommand* cmd = FW_NEW(CChangePatternCommand,
  884.                                                     (ev, cChangeFramePattern, this, this->GetLastActiveFrame(ev), fDrawSelection, pattern));
  885.         cmd->Execute(ev);
  886.     }
  887. }
  888.  
  889. //----------------------------------------------------------------------------------------
  890. // CDrawPart::WhichShape
  891. //----------------------------------------------------------------------------------------
  892.  
  893. CBaseShape* CDrawPart::WhichShape(Environment *ev, FW_CGraphicContext& gc, const FW_CMouseEvent& theMouseEvent, FW_Boolean hasToBeSelected) const
  894. {
  895.     CDrawContentShapeIterator ite(fPartContent);
  896.     for (CBaseShape* shape = ite.Last(); ite.IsNotComplete(); shape = ite.Previous())
  897.     {
  898.         FW_Boolean test = (!hasToBeSelected) || (hasToBeSelected && shape->IsSelectedShape());
  899.         if (test && shape->HitTest(ev, gc, theMouseEvent))
  900.             return shape;    
  901.     }
  902.     
  903.     return NULL;
  904. }
  905.  
  906. //----------------------------------------------------------------------------------------
  907. // CDrawPart::FindShapeWithIndex
  908. //----------------------------------------------------------------------------------------
  909. CBaseShape* CDrawPart::FindShapeWithIndex(short extIndex)
  910. {
  911.     // Search the shape list and return the shape with the specified externalization index
  912.     CDrawContentShapeIterator ite(fPartContent);
  913.     for (CBaseShape* shape = ite.First(); ite.IsNotComplete(); shape = ite.Next())
  914.     {
  915.         if (shape->GetExternalizationIndex() == extIndex)
  916.             return shape;
  917.     }    
  918.  
  919.     // extIndex not found!?
  920.     return NULL;
  921. }
  922.  
  923. //----------------------------------------------------------------------------------------
  924. // CDrawPart::DoPostCreate
  925. //----------------------------------------------------------------------------------------
  926. void CDrawPart::DoPostCreate(Environment* ev)
  927. {
  928.     // Perform "DoPostCreate"-type initialization.
  929.     // In our case, we want to make sure that any subscribers that were just internalized 
  930.     // get registered for automatic updates.
  931.  
  932.     if (!fPostCreated)    // do this only once!
  933.     {
  934.         CDrawLinkManager* linkMgr = (CDrawLinkManager*) this->GetLinkManager(ev);
  935.         linkMgr->RegisterLinks(ev);
  936.         fPostCreated = TRUE;
  937.     }
  938. }
  939.  
  940. //----------------------------------------------------------------------------------------
  941. // CDrawPart::NewElementIterator
  942. //----------------------------------------------------------------------------------------
  943.  
  944. FW_CElementIterator* CDrawPart::NewElementIterator(Environment* ev,
  945.                                                         FW_CPart* part,
  946.                                                         ODDescType elementType) const
  947. {
  948.     FW_CElementIterator* iter;
  949.     
  950.     if (elementType == kShapeClass)
  951.         iter = FW_NEW(CSemanticShapeElementIterator, (fPartContent));
  952.     else
  953.         iter = FW_MEmbeddingPartScriptable::NewElementIterator(ev, part, elementType);
  954.         
  955.     return iter;
  956. }
  957.  
  958. //----------------------------------------------------------------------------------------
  959. // CDrawPart::DoAECreateElement
  960. //----------------------------------------------------------------------------------------
  961.  
  962. void CDrawPart::DoAECreateElement(Environment* ev,
  963.                                 FW_CPart* part,
  964.                                 const FW_CAppleEvent& event,
  965.                                 FW_CAppleEvent& reply)
  966. {
  967.     FW_CDesc         propertyValues;
  968.     unsigned short     newShapeType = kRectShape;
  969.     
  970.     if (event.HasDataKey(keyAEPropData))
  971.         event.GetDataByDesc(propertyValues, keyAEPropData, typeAERecord);
  972.         
  973.     if (propertyValues.HasDataKey(kShapeTypeProperty))
  974.     {
  975.         ODDescType shapeType;
  976.         shapeType = propertyValues.GetEnumeratedType(kShapeTypeProperty);
  977.         
  978.         switch(shapeType)
  979.         {
  980.             case kLineShapeType:
  981.                 newShapeType = kLineShape;
  982.                 break;
  983.             
  984.             case kRectShapeType:
  985.                 newShapeType = kRectShape;
  986.                 break;
  987.             
  988.             case kOvalShapeType:
  989.                 newShapeType = kOvalShape;
  990.                 break;
  991.             
  992.             case kRRectShapeType:
  993.                 newShapeType = kRRectShape;
  994.                 break;
  995.                 
  996.             case kTextShapeType:
  997.                 newShapeType = kTextShape;
  998.                 break;
  999.             
  1000.             default:
  1001.                 break;
  1002.         }
  1003.         propertyValues.DeleteListItem(kShapeTypeProperty);
  1004.     }    
  1005.     
  1006.     CBaseShape* theShape = GetDrawContent()->NewShape(ev, newShapeType);
  1007.     
  1008.     FW_CColor color;
  1009.     
  1010.     theShape->SetFrameInk(FW_kNormalInk);
  1011.     theShape->SetFrameStyle(FW_kNormalStyle);
  1012.     
  1013.     theShape->ChangeRenderVerb(ev, this, GetRenderVerb());
  1014.     theShape->ChangePenSize(ev, this, GetPenSize());
  1015.             
  1016.     GetFrameColor(color);
  1017.     theShape->ChangeFrameColor(ev, this, color);
  1018.  
  1019.     GetFillColor(color);
  1020.     theShape->ChangeFillColor(ev, this, color);
  1021.  
  1022.     theShape->ChangeFramePattern(ev, this, GetFramePattern());
  1023.     theShape->ChangeFillPattern(ev, this, GetFillPattern());
  1024.     
  1025.     FW_CPoint topLeft(FW_IntToFixed(10), FW_IntToFixed(10));
  1026.     FW_CPoint botRight(FW_IntToFixed(82), FW_IntToFixed(82));
  1027.     
  1028.     theShape->SetShapeGeometry(topLeft, botRight);
  1029.  
  1030.     theShape->SetProperties(ev, part, propertyValues);
  1031.  
  1032.     CDrawShapeCommand* cmd = FW_NEW(CDrawShapeCommand,
  1033.                     (ev, this, GetFrame(ev, part), fDrawSelection, theShape));
  1034.     
  1035.     cmd->Execute(ev);
  1036.     
  1037.     GetDrawContent()->RedrawShape(ev, theShape);
  1038.     
  1039.     FW_CDesc objectSpec;
  1040.     
  1041.     BuildObjectSpecifier(ev, part, GetFrame(ev, part), objectSpec, GetSpecifierForm());
  1042.     reply.PutDataByDesc(objectSpec, keyAEResult);
  1043. }
  1044.  
  1045. //----------------------------------------------------------------------------------------
  1046. // CDrawPart::EmbeddedFrameRemoved
  1047. //----------------------------------------------------------------------------------------
  1048.  
  1049. void CDrawPart::EmbeddedFrameRemoved(Environment *ev, FW_MProxy* proxy)
  1050. {
  1051.     CProxyShape* proxyShape = (CProxyShape*)proxy;
  1052.     
  1053.     FW_CRect updateBox;
  1054.     proxyShape->GetUpdateBox(updateBox);
  1055.     
  1056.     proxyShape->GetPresentation(ev)->Invalidate(ev, updateBox);
  1057.     
  1058.     RemoveShapeFromPart(ev, proxyShape);
  1059.     
  1060.     delete proxyShape;
  1061. }
  1062.